package cn.wit.summit.chwan.Stat;

import android.content.Context;
import android.content.Intent;

import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;
import java.util.Map;

import cn.wit.summit.chwan.dot.DotRequestManager;
import cn.wit.summit.chwan.dot.DotService;
import cn.wit.summit.chwan.models.db.DotRequestTable;
import cn.wit.summit.chwan.models.db.DownLoadTaskTable;
import cn.wit.summit.chwan.models.dbmanager.DownloadTaskManager;
import cn.wit.summit.chwan.ui.service.MyJobIntentDotService;
import cn.wit.summit.chwan.utils.JsonMapper;
import cn.wit.summit.chwan.utils.LogUtils;
import cn.wit.summit.chwan.utils.MobileCodeUtils;
import cn.wit.summit.chwan.utils.StringUtils;

public class StatFactory {
    private static StatFactory statFactory;

    private StatFactory() {
    }

    public static StatFactory getInstance() {
        if (statFactory == null) {
            statFactory = new StatFactory();
        }
        return statFactory;
    }

    //下载中，status：1.成功;2.失败;3.请求;4.暂停;5.删除
    public void sendGameDownload(Context context, String gameId, String status, boolean isGamedetial) {

        DownLoadTaskTable taskTable = DownloadTaskManager.getInstance().findByGameId(gameId);
        StatGameDownload stat = new StatGameDownload(context);
        stat.setGameId(gameId);
        if (taskTable != null && taskTable.getDownspeed() != null) {
            stat.setOpSpeed(taskTable.getDownspeed());
        }
        stat.setOpStatus(status);
        if (isGamedetial) {
            stat.setLoadWay(2 + "");
        } else {
            stat.setLoadWay(1 + "");
        }
        StatMainBean<StatGameDownload> mainBean = new StatMainBean<>();
        mainBean.setData(stat);
        mainBean.setEvent(Event.gameDownload.name());
        sendPoint(context, JsonMapper.getInstance().toJson(mainBean));

        String umEvntId = "";
        if ("1".equals(status)) {
            umEvntId = "9001";
        } else if ("2".equals(status)) {
            umEvntId = "9002";
        } else if ("3".equals(status)) {
            umEvntId = "9003";
        } else if ("4".equals(status)) {
            umEvntId = "9004";
        } else if ("5".equals(status)) {
            umEvntId = "9005";
        }
        String gameName = gameId;
        if (taskTable != null && StringUtils.isEmpty(taskTable.getGameName())) {
            gameName = taskTable.getGameName();
        }
        sendUMengPoint(context, gameId, gameName, umEvntId);
    }

    //解压中
    public void sendUnZip(Context context, String gameId, String status) {
        DownLoadTaskTable taskTable = DownloadTaskManager.getInstance().findByGameId(gameId);
        StatGameDownload stat = new StatGameDownload(context);
        stat.setGameId(gameId);
        if (taskTable != null) {
            stat.setOpSpeed(taskTable.getDownspeed());
        }
        stat.setOpStatus(status);
        stat.setOpSpeed(taskTable.getDownspeed());
        StatMainBean<StatGameDownload> mainBean = new StatMainBean<>();
        mainBean.setData(stat);
        mainBean.setEvent(Event.gameUnzip.name());
        sendPoint(context, JsonMapper.getInstance().toJson(mainBean));
        String gameName = gameId;
        if (taskTable != null && StringUtils.isEmpty(taskTable.getGameName())) {
            gameName = taskTable.getGameName();
        }
        if ("3".equals(status))
            sendUMengPoint(context, gameId, gameName, "9006");
    }

    //1.成功;2.失败;3.请求
    //解压失败
    public void sendUnZipError(Context context, String gameId, String status,String errorMessage) {
        DownLoadTaskTable taskTable = DownloadTaskManager.getInstance().findByGameId(gameId);
        StatGameDownload stat = new StatGameDownload(context);
        stat.setGameId(gameId);
        if (taskTable != null) {
            stat.setOpSpeed(taskTable.getDownspeed());
        }
        stat.setMessage(errorMessage);
        stat.setOpStatus(status);
        stat.setOpSpeed(taskTable.getDownspeed());
        StatMainBean<StatGameDownload> mainBean = new StatMainBean<>();
        mainBean.setData(stat);
        mainBean.setEvent(Event.gameUnzip.name());
        sendPoint(context, JsonMapper.getInstance().toJson(mainBean));

        String gameName = gameId;
        if (taskTable != null && StringUtils.isEmpty(taskTable.getGameName())) {
            gameName = taskTable.getGameName();
        }
        if ("2".equals(status))
            sendUMengPoint(context, gameId, gameName, "9007");
    }

    //解压成功
    public void sendUnZipSuccess(Context context, String gameId, String status, String speed) {
        DownLoadTaskTable taskTable = DownloadTaskManager.getInstance().findByGameId(gameId);
        StatGameDownload stat = new StatGameDownload(context);
        stat.setGameId(gameId);
        stat.setOpSpeed(speed);
        stat.setOpStatus(status);
        stat.setOpSpeed(taskTable.getDownspeed());
        StatMainBean<StatGameDownload> mainBean = new StatMainBean<>();
        mainBean.setData(stat);
        mainBean.setEvent(Event.gameUnzip.name());
        sendPoint(context, JsonMapper.getInstance().toJson(mainBean));

        String gameName = gameId;
        if (taskTable != null && StringUtils.isEmpty(taskTable.getGameName())) {
            gameName = taskTable.getGameName();
        }
        if ("1".equals(status))
            sendUMengPoint(context, gameId, gameName, "9008");
    }

    //1.成功;2.失败;3.请求;4.卸载
    //游戏安装 status
    public void sendGameInstall(Context context, String gameId, String status) {
        String umEvntId = "";
        if ("1".equals(status)) {
            umEvntId = "9009";
            MobileCodeUtils.taskDownloadGameTag(context, gameId, 2);
        } else if ("2".equals(status)) {
            umEvntId = "9010";
        } else if ("3".equals(status)) {
            umEvntId = "9011";
        }
        DownLoadTaskTable taskTable = DownloadTaskManager.getInstance().findByGameId(gameId);
        StatGameDownload stat = new StatGameDownload(context);
        stat.setGameId(gameId);
        if (taskTable != null) {
            stat.setOpSpeed(taskTable.getDownspeed());
        }
        stat.setOpStatus(status);

        StatMainBean<StatGameDownload> mainBean = new StatMainBean<>();
        mainBean.setData(stat);
        mainBean.setEvent(Event.gameInstall.name());
        sendPoint(context, JsonMapper.getInstance().toJson(mainBean));
        String gameName = gameId;
        if (taskTable != null && StringUtils.isEmpty(taskTable.getGameName())) {
            gameName = taskTable.getGameName();
        }
        sendUMengPoint(context, gameId, gameName, umEvntId);
    }

    public void sendGameStart(Context context, String gameId) {
        MobileCodeUtils.taskDownloadGameTag(context, gameId, 3);
        StatGameDownload stat = new StatGameDownload(context);
        stat.setGameId(gameId);
        stat.setOpStatus(1 + "");
        StatMainBean<StatGameDownload> mainBean = new StatMainBean<>();
        mainBean.setData(stat);
        mainBean.setEvent(Event.gameStart.name());
        sendPoint(context, JsonMapper.getInstance().toJson(mainBean));
    }

    public void sendGameGameDetial(Context context, String gameId, Event event) {
        VistPage stat = new VistPage(context);
        stat.setGroupName("gameDetail");
        stat.setPageName("gameDetail");
        stat.setGameId(gameId);
        StatMainBean<VistPage> mainBean = new StatMainBean<>();
        mainBean.setData(stat);
        mainBean.setEvent(event.name());
        sendPoint(context, JsonMapper.getInstance().toJson(mainBean));
    }

    public void sendVistPage(Context context, String groupName, String pageName) {
        VistPage stat = new VistPage(context);
        stat.setGroupName(groupName);
        stat.setPageName(pageName);

        StatMainBean<VistPage> mainBean = new StatMainBean<>();
        mainBean.setData(stat);
        mainBean.setEvent(Event.vistPage.name());
        LogUtils.e("stat sendVistPage");
        sendPoint(context, JsonMapper.getInstance().toJson(mainBean));
    }

    public void sendClickPage(Context context, String groupName, String pageName) {
        VistPage stat = new VistPage(context);
        stat.setGroupName(groupName);
        stat.setPageName(pageName);

        StatMainBean<VistPage> mainBean = new StatMainBean<>();
        mainBean.setData(stat);
        mainBean.setEvent(Event.clickPage.name());
        LogUtils.e("stat sendVistPage");
        sendPoint(context, JsonMapper.getInstance().toJson(mainBean));
    }

    public void sendClickAdvertSpacePage(Context context, String firstMode, String firstName
            , String secondMode, String secondName, int position) {
        StatPosition stat = new StatPosition(context);
        stat.setFirstMode(firstMode);
        stat.setFirstName(firstName);
        stat.setSecondMode(secondMode);
        stat.setSecondName(secondName);
        stat.setPosition(position + "");

        StatMainBean<StatPosition> mainBean = new StatMainBean<>();
        mainBean.setData(stat);
        mainBean.setEvent(Event.clickAdvertSpace.name());
        LogUtils.e("stat sendClickAdvertSpacePage");
        sendPoint(context, JsonMapper.getInstance().toJson(mainBean));
    }

    public void sendShare(Context context, String gameId, String from, String fromName) {
        StatShare stat = new StatShare(context);
        stat.setGameId(gameId);

        stat.setOpChannel(from);
        stat.setOpName(fromName);
        StatMainBean<StatShare> mainBean = new StatMainBean<>();
        mainBean.setData(stat);
        mainBean.setEvent(Event.appShare.name());
        LogUtils.e("stat sendGameGameDetial");
        sendPoint(context, JsonMapper.getInstance().toJson(mainBean));
    }

    public void startApp(Context context, String shareSource, String opStatus) {
        StartApp stat = new StartApp(context);
        stat.setShareSource(shareSource);
        stat.setOpStatus(opStatus);

        StatMainBean<StartApp> mainBean = new StatMainBean<>();
        mainBean.setData(stat);
        mainBean.setEvent(Event.startAPP.name());
        LogUtils.e("stat startAPP()");
        sendPoint(context, JsonMapper.getInstance().toJson(mainBean));
    }

    public void sendGameDownLoadDone(Context context, String gameId) {
        StatGameDownload stat = new StatGameDownload(context);
        stat.setGameId(gameId);
        DownLoadTaskTable taskTable = DownloadTaskManager.getInstance().findByGameId(gameId);
        if (taskTable != null && taskTable.getDownspeed() != null) {
            stat.setOpSpeed(taskTable.getDownspeed());
        }
        stat.setOpStatus(1 + "");
        StatMainBean<StatGameDownload> mainBean = new StatMainBean<>();
        mainBean.setData(stat);
        mainBean.setEvent(Event.gameDownDone.name());
        sendPoint(context, JsonMapper.getInstance().toJson(mainBean));
    }
    /*****************************************************************************************************************/

    public void sendPoint(Context activity, String json) {
        DotRequestTable table = new DotRequestTable();
        table.setJsonData(json);
        DotRequestManager.getInstance().insertDownload(table);
        Intent intent = new Intent(activity, DotService.class);
        activity.startService(intent);
//        MyJobIntentDotService.enqueueWork(activity, intent);
    }

    public void sendUMengPoint(Context context, String gameId, String gameName, String eventId) {
        if (StringUtils.isEmpty(eventId)) return;
        Map<String, String> map = new HashMap<>();
        map.put("gameid", gameId);
        map.put("gameName", gameName);
        MobclickAgent.onEvent(context, eventId, map);
    }
}
